package com.guli.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.service.AttrService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;



/**
 * 商品属性
 *
 * @author wzh
 * @email sunlightcs@gmail.com
 * @date 2021-12-09 15:49:12
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/{type}/list/{id}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("type") String type,@PathVariable("id") Integer id){
        PageUtils page = attrService.queryPage(params,type,id);

        return R.ok().put("page", page);
    }

    /**
     * 通过分组id查询
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Integer attrGroupId){
        List<AttrEntity> data = attrService.attrRelation(attrGroupId);

        return R.ok().put("data", data);
    }

    /**
     * 查询被分组关联的规格属性
     */
    @RequestMapping("/noattr/relation")
    public R noattrRelation(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.noattrRelation(params);

        return R.ok().put("page", page);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getByAid(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrEntity attr){
		attrService.add(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrEntity attr){
		attrService.updById(attr);

        return R.ok();
    }

    /**
     * 解除关联
     */

    /**
     * 关联分组
     */
    @RequestMapping("/relation")
    public R relation(@RequestBody List<AttrEntity> attrEntities){

		attrService.updateBatchById(attrEntities);

        return R.ok();
    }

    /**
     * 解绑关联
     */
    @RequestMapping("/relation/update")
    public R relationUpdate(@RequestBody AttrEntity attrEntities){

		attrService.relationUpdate(attrEntities);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
